package org.NooLab.itexx.app.gui;

import java.awt.Component;
import java.io.IOException;

import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.gui.handlers.PanelSearchParameters;
import org.NooLab.itexx.app.gui.handlers.PanelText;
import org.NooLab.utilities.strings.StringsUtil;

public class StackedPanelProducer {

	
	public static final int _EXTEND = -999999;
	public static final int _INSERT = -9;
	
	StackedPanels stackedPanels;
	ITexxWindowCommons appCommons;
	boolean cssStyling = false;
	
	public StringsUtil strgutil = new StringsUtil();
	private PanelSearchParameters searchParams;
	private String cssDefinitions;
	// ==============================================================
	public StackedPanelProducer(ITexxWindowCommons appCommons){
	
		this.appCommons = appCommons;
		stackedPanels = new StackedPanels (appCommons);
		
		
		appCommons.setStackedPanelProducer(this) ;
	}
	// ==============================================================

	public void setCssStylingActive(boolean flag, String resourceFilePath){
		
		//
		String _cssDefinitions = "";
		
		// css is defined in resources, 
		
		
		
		if (_cssDefinitions.length()>5){
			cssDefinitions = _cssDefinitions ;
			cssStyling = true;
		}
	}
	
	public StackedPanels getPanels() {
		
		return stackedPanels;
	}

	public void repaintAll(float transparency) {
		// 
		StackedPanel sp ;
		
		for (int i=0;i<stackedPanels.size();i++){
			sp = stackedPanels.getItem(i);
			sp.combiPanel.setOpaque(true);
			sp.combiPanel.setTransparency(transparency);
			sp.combiPanel.repaint();
		}
		
	}


	public void clearPanels(int remaining) {
		//
		int n = stackedPanels.size();
		for (int i=n-1;i>=remaining;i--){
			stackedPanels.remove(i) ;
		}
		
	}

	public void setSearchParameters( PanelSearchParameters searchparams){
		searchParams = new PanelSearchParameters(searchparams) ;
	}
	public StackedPanel[] searchForPanels( String searchForStr ) {
		
		StackedPanel sp;
		StackedPanels sps = new StackedPanels(appCommons);
		StackedPanel[] spa = new StackedPanel[0]; 
		PanelText pT;
		String text;
		
		
		for (int i=0;i<stackedPanels.size();i++){
			sp = stackedPanels.getItem(i);
			pT = sp.getPanelText();
			
			// searchParams ...
			text = pT.getText();
			if (text.indexOf(searchForStr)>=0){
				sps.add(sp) ;
				// also checking the source ?
			}
		}
		
		spa = new StackedPanel[ sps.size()];
		for (int i=0;i<sps.size();i++){
			spa[i] = sps.getItem(i) ;
		}
		sps.items.clear() ;
		
		return spa;
	}


	public StackedPanel[] searchForPanels(String[] searchForStrs) {
		//
		StackedPanel sp;
		StackedPanels sps = new StackedPanels(appCommons);
		StackedPanel[] spa = new StackedPanel[0]; 
		PanelText pT;
		String text,searchForStr;
		
		
		for (int i=0;i<stackedPanels.size();i++){
			sp = stackedPanels.getItem(i);
			pT = sp.getPanelText();
			text = pT.getText();
			// searchParams ...
			for (int k=0;k<searchForStrs.length;k++){
				
				searchForStr = searchForStrs[k] ;
				
				if (text.indexOf(searchForStr) >= 0) {
					sps.add(sp);
					// also checking the source ?
					// relevancy by multiple match ?
					// FIXME if we have an OR search, we may break, but NOT, if we are in an AND search
					break;
				}
			}
		}
		
		spa = new StackedPanel[ sps.size()];
		for (int i=0;i<sps.size();i++){
			spa[i] = sps.getItem(i) ;
		}
		sps.items.clear() ;
		
		return spa;
	}

	
	/**
	 *  TODO  requires session object
	 */
	public void saveAsSession(String sessionname){
		
	}
	

	/**
	 * it returns the index values of the inserted items
	 */
	public int[] extendPanels( int count ) {
		int[] result = new int[count];
		
		int mode = _EXTEND ;
		
		for (int i=0;i<count;i++){
			
			// create one and add it
			

		}// i->
		
		return result ;
	}

	public int insertPanels(int count, int insertPosition) {
		int result = -1;
		// 
		
		int mode = _INSERT;
		
		return result ;
	}

	public StackedPanel addPanel(int position, String str) {
		StackedPanel sp;
		
		sp = addPanel( position );
		
		sp.getPanelText().setText(str);
		
		sp.update();
		
		appCommons.getFrame().validate() ;
		
		return sp;
	}
	
	public StackedPanel addPanel(){
		return addPanel( _EXTEND );
	}
	
	public StackedPanel addPanel( int position ){
		
		StackedPanel sp=null;
		try {
			
			sp = new StackedPanel(this, stackedPanels);
			/*
			 * if we insert an item at an arbitrary position p, we add the physical
			 * panel at the end, but we return the panel at the position p
			 * 
			 * for all other panels we relocate the content
			 * 
			 * 
			 */
			if (position==_EXTEND ){
				
				stackedPanels.add(sp) ;	
			}
			if (position>=0){
			
				sp = stackedPanels.insert(position, sp);
				sp.combiPanel.updateText() ;
			}
			
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		
		
		return sp;
	}
	
	public void provideMissingPanels(int index) {
		// 
		
		while (index>=stackedPanels.items.size()){
			addPanel(_EXTEND) ;
		}
		
	}
	public void removePanels(int position, int count) {
		
		
	}

	public StringsUtil getStrgutil() {
		return strgutil;
	}


	public int panelCount() {
		int nc=-1;
		if (stackedPanels!=null){
			nc = stackedPanels.size();
		}
		return nc;
	}


	public boolean isCssStyling() {
		return cssStyling;
	}

	public void setCssStyling(boolean cssStyling) {
		this.cssStyling = cssStyling;
	}

	public String getCssDefinitions() {
		return cssDefinitions;
	}

	public void setCssDefinitions(String cssDefinitions) {
		this.cssDefinitions = cssDefinitions;
	}

	public ITexxWindowCommons getAppCommons() {
		return appCommons;
	}
	
	
}
